package ch08_Swing高级组件.table;

import ch07_GUI编程.BaseFrame;

import javax.swing.*;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableRowSorter;

public class JTableDemo {
	public static void main(String[] args) {
		BaseFrame f = new BaseFrame("JTable Demo");

		JTableDemo demo = new JTableDemo();
		JScrollPane sp  = new JScrollPane(demo.createTable());		//创建表格并加入可滚动面板
		sp.setSize(500,140);
		sp.setLocation(10, 10);

		f.add(sp);
		f.showMe();
	}
	JTable createTable() {
		StudentTableModel model = new StudentTableModel();			//构造表格模型
		JTable table = new JTable(model);							//已指定模型构造表格
		TableRowSorter sorter = new TableRowSorter(model);			//构造表格行排列器
		table.setRowSorter(sorter);									//设置行排列器
		table.setRowHeight(20);										//设置表格的行高
		this.setCellRendererAndEditor(table);
		this.setColumnWidth(table);
		return table;												//返回表格组件

	}
	//设置表格渲染器和编辑器
	void setCellRendererAndEditor(JTable table) {
		TableColumnModel colModel = table.getColumnModel();					//得到表格模型
		StudentTableCellRenderer renderer = new StudentTableCellRenderer();	//构造渲染器

		for (int i = 0; i < colModel.getColumnCount(); i++) {				//设置每一列渲染器
			colModel.getColumn(i).setCellRenderer(renderer);
		}
		TableColumn majorCol = colModel.getColumn(3);				//得到专业列
		JComboBox cb = new JComboBox(StudentTableModel.majors);		//构造下拉列表
		DefaultCellEditor editor = new DefaultCellEditor(cb);		//单元格编辑器
		majorCol.setCellEditor(editor);								//设置专业列的编辑器
	}
	//设置表格列宽度
	void setColumnWidth(JTable table) {
		TableColumnModel colModel = table.getColumnModel();
		for (int i = 0; i < colModel.getColumnCount(); i++) {
			if(i==0||i==2) {	//设置编号、年龄列的最小/最大宽度
				colModel.getColumn(i).setMinWidth(30);
				colModel.getColumn(i).setMaxWidth(50);
			}
		}
	}
}
